package ha.web.client.servlet.runGame;

import ha.web.server.exceptions.GameException;
import ha.web.server.manager.GameMangerServerSide;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class show_statistics
 */
@WebServlet("/ShowStatistics")
public class ShowStatistics extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowStatistics() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userId = -1;
		String nickName = null;
		boolean cookieFound = false;
		Cookie[] userCookie = request.getCookies();  
		for(int i=0; i<userCookie.length; i++) {
			if ("chinese_checkers".equals(userCookie[i].getName())) {
				String[] cookie = userCookie[i].getValue().split(":");
				nickName = cookie[0];
				userId = Integer.parseInt(cookie[1]);
				cookieFound = true;
			}
		}
		
		/*
		 *form.setAttribute("game_data", answer[1]);
		form.setAttribute("at_mid_game","false"); 
		 * */
		if(!cookieFound)
		{
			ServletContext context = getServletContext();
			RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
			dispatcher.forward(request, response);
		}else{
			GameMangerServerSide ins = GameMangerServerSide.getInstance();
			boolean userExistFlag = false;
			String statistic = null;
			String gameStat = null;
			String needToRet = null;
			try {
				userExistFlag = ins.isUserAtDB(nickName,userId);
				statistic = ins.getUserStatistics(nickName,userId);
				gameStat = request.getParameter("game_data");
				needToRet = request.getParameter("at_mid_game");
				if(gameStat==null || needToRet==null)
					throw new GameException("ERROR");
			} catch (GameException e) {
				forwardToLogin(request,response);
				return;
			}
			if (userExistFlag) {
				PrintWriter out = response.getWriter();
				out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
				out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
				out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");  
				out.print("<h2>Current user statistics:</h2><br>");
				out.print(gameStat);
				out.print("<br><br><br><br>"+statistic);
				if (needToRet.equals("true")) {
					out.print("<br><input action=\"action\" type=\"button\" onclick=\"history.go(-1)\"value=\"Return To Game\" </button>");
				}else {
					out.print("<br><button type=\"button\" class=\"button\" onclick=window.location=\"login\"; }>go to login</button>");
				}
				out.print("</div>");
				out.print("</BODY></HTML>");
			} else {
				forwardToLogin(request,response);
			}
			

		}

	}
	
	private void forwardToLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext context = getServletContext();
		RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
		dispatcher.forward(request, response);	
		return;

	}
}
